﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Minimap
{
    using System.Windows.Ink;

    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class MinimapView : UserControl
    {
        private Point startPoint;

        /// <summary>
        /// Initializes a new instance of the <see cref="MinimapView"/> class.
        /// </summary>
        public MinimapView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the diff.
        /// </summary>
        /// <value>The diff.</value>
        private Vector Diff { get; set; }

        /// <summary>
        /// Gets or sets the thumbnail.
        /// </summary>
        /// <value>The thumbnail.</value>
        private Visual Thumbnail { get; set; }

        private Color MinimapHandleActiveColor { get; set; }

        private ScrollViewer TreeScroll { get; set; }

        private double OriginalElementHeight { get; set; }

        private double OriginalElementWidth { get; set; }

        private Visual VisualElement { get; set; }

        /// <summary>
        /// Setups the specified visual element.
        /// </summary>
        /// <param name="visualElement">The visual element.</param>
        /// <param name="originalElementWidth">Width of the original element.</param>
        /// <param name="originalElementHeight">Height of the original element.</param>
        /// <param name="treeScroll">The tree scroll.</param>
        public void Setup(Visual visualElement, double originalElementWidth, double originalElementHeight, ScrollViewer treeScroll)
        {
            this.VisualElement = visualElement;
            this.OriginalElementWidth = originalElementWidth;
            this.OriginalElementHeight = originalElementHeight;
            this.TreeScroll = treeScroll;

            this.Calibrate();
        }

        public void Calibrate()
        {
            this.UpdateLayout();
            double d = this.OriginalElementWidth / Application.Current.MainWindow.ActualWidth;
            double h = this.OriginalElementHeight / Application.Current.MainWindow.ActualHeight;

            this.MinimapHandle.Width = (this.MinimapNavigationArea.ActualWidth / d) - 4;
            this.MinimapHandle.Height = (this.MinimapNavigationArea.ActualHeight / h) - 8;

            this.MinimapThumbnail.Fill = new VisualBrush(this.VisualElement);
        }

        /// <summary>
        /// Handles the mouse down.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void HandleMouseDown(object sender, MouseButtonEventArgs e)
        {
            this.startPoint = e.GetPosition(this.MinimapHandle);
        }

        /// <summary>
        /// Areas the mouse move.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void AreaMouseMove(object sender, MouseEventArgs e)
        {
            Vector moveVector = e.GetPosition(this.MinimapNavigationArea) - this.startPoint;
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (moveVector.X >= 0 && moveVector.X <= this.MinimapNavigationArea.ActualWidth - this.MinimapHandle.ActualWidth)
                {
                    Canvas.SetLeft(this.MinimapHandle, moveVector.X);
                }

                if (moveVector.Y >= 0 && moveVector.Y <= this.MinimapNavigationArea.ActualHeight - this.MinimapHandle.ActualHeight)
                {
                    Canvas.SetTop(this.MinimapHandle, moveVector.Y);
                }

                double d = this.OriginalElementWidth / this.MinimapNavigationArea.ActualWidth;
                double h = this.OriginalElementHeight / this.MinimapNavigationArea.ActualHeight;

                this.TreeScroll.ScrollToHorizontalOffset(d * moveVector.X);
                this.TreeScroll.ScrollToVerticalOffset(h * moveVector.Y);

                this.MinimapHandle.Stroke = new SolidColorBrush(Colors.Orange);
            }

            this.MinimapHandle.Stroke = new SolidColorBrush(Colors.Red);
        }
    }
}
